//
//  MapManager.m
//  tcpoctest
//
//  Created by gump on 11/20/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "MapManager.h"

BMKMapManager* _mapManager = nil;

@implementation MapManager

static MapManager *sharedInstance = nil;

+(MapManager*)shareControl
{
    if(!sharedInstance)
    {
        sharedInstance = [[super alloc] init];
    }
    return sharedInstance;
}

-(id)init
{
    self = [super self];
    if(self)
    {
        locationManager = nil;
        mmapView = nil;
    }
    return self;
}

-(void)initOSMap
{
    if (nil == locationManager)
    {
        locationManager = [[CLLocationManager alloc] init];
    }
    
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    
    locationManager.distanceFilter = 500;
    [locationManager startUpdatingLocation];
}

#pragma mark -os map delegate-
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    // If it's a relatively recent event, turn off updates to save power
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 5.0)
    {
        [manager stopUpdatingLocation];
        
        printf("locationManager latitude %+.6f, longitude %+.6f\n",
               newLocation.coordinate.latitude,
               newLocation.coordinate.longitude);
    }
    // else skip the event and process the next one.
    CLLocationCoordinate2D loc = [newLocation coordinate];
    [DataControl shareControl].mlatitude = loc.latitude * 1000000;
    [DataControl shareControl].mlongitude = loc.longitude * 1000000;
    MAP_LOG(@"mapView %f %f", loc.latitude, loc.longitude);
}

-(void)initBaiduMap
{
    if (_mapManager == nil) {
        _mapManager = [[BMKMapManager alloc]init];
    }
    
	BOOL ret = [_mapManager start:@"5FE1BF6C3657AC7DFD5E6353245EF30EB04F2EB4" generalDelegate:self];
	if (!ret) {
		[self initOSMap];
	}
    else
    {
        if (mmapView == nil) {
            mmapView = [[BMKMapView alloc] init];
        }

        mmapView.showsUserLocation = YES;
        mmapView.delegate = self;
    }
}

#pragma mark -baidumap fun-
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
	if (userLocation != nil) {
		MAP_LOG(@"baidu  map %f %f",
              userLocation.location.coordinate.latitude,
              userLocation.location.coordinate.longitude);
        
        [DataControl shareControl].mlatitude = userLocation.location.coordinate.latitude * 1000000;
        [DataControl shareControl].mlongitude = userLocation.location.coordinate.longitude * 1000000;
        
        mapView.showsUserLocation = NO;
        
        [_mapManager stop];
	}
    
    
}

- (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
	if (error != nil)
		MAP_LOG(@"locate failed: %@", [error localizedDescription]);
	else {
		MAP_LOG(@"locate failed");
	}
    
    [_mapManager stop];
    
    [self initOSMap];
	
}

@end
